//
//  NSDictionaryAdditions.java
//  WOGroovy
//

package groovy.lang.webobjects.categories;

import com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import er.extensions.*;
import java.util.*;
import groovy.lang.*;
import groovy.util.MapEntry;

import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;


public class NSDictionaryAdditions {
	
    //
    // accessing
    //
	
    public static Object getAt(NSDictionary self, Object key) {
        return self.objectForKey(key);
    }
    
    //
    // enumerating
    //
    
    public static void each(NSDictionary self, Closure closure) {
    	for (Enumeration e = self. keyEnumerator(); e.hasMoreElements();) {
        	Object key = e.nextElement();
        	Map.Entry entry = new MapEntry(key, self.objectForKey(key));
        	if (closure.getMaximumNumberOfParameters() == 2) {
                closure.call(new Object[]{entry.getKey(), entry.getValue()});
            } else {
            	closure.call(entry);
            }
        }
    }
    
    public static void eachKey(NSDictionary self, Closure closure) {
        for (Enumeration e = self.keyEnumerator(); e.hasMoreElements();) {
            closure.call(e.nextElement());
        }
    }
    
    public static void eachValue(NSDictionary self, Closure closure) {
        for (Enumeration e = self.objectEnumerator(); e.hasMoreElements();) {
            closure.call(e.nextElement());
        }
    }

    public static NSArray collect(NSDictionary self, Closure closure) {
        NSMutableArray answer = new NSMutableArray();
        for (Enumeration e = self.keyEnumerator(); e.hasMoreElements();) {
        	Object key = e.nextElement();
        	Map.Entry entry = new MapEntry(key, self.objectForKey(key));
            answer.addObject(closure.call(entry));
        }
        return new NSArray(answer);
    }

    public static Object find(NSDictionary self, Closure closure) {
        for (Enumeration e = self.keyEnumerator(); e.hasMoreElements();) {
        	Object key = e.nextElement();
        	Map.Entry entry = new MapEntry(key, self.objectForKey(key));
            if (DefaultTypeTransformation.castToBoolean(closure.call(entry))) {
               return entry;
            }
        }
        return null;
    }

    //
    // instance creation
    //
    
    public static NSDictionary
    		dictionaryByRemovingFromDictionaryKeysInArray(NSDictionary self, List array) {
    	NSArray list;
    	if (array instanceof NSArray) {
    		list = (NSArray)array;
    	} else {
    		list = new NSArray(array, false);
    	}
        return ERXDictionaryUtilities.
            dictionaryByRemovingFromDictionaryKeysInArray(self, list);
    }
    
    public static NSDictionary
        dictionaryWithDictionary(NSDictionary self, NSDictionary other) {
        return ERXDictionaryUtilities.
            dictionaryWithDictionaryAndDictionary(self, other);
    }
    
    public static Map map(NSDictionary self) {
        return new HashMap(self.hashtable());
    }

    public static NSArray sortOrderings(NSDictionary self) {
        NSMutableArray orderings = new NSMutableArray();
        for (Enumeration e = self.keyEnumerator(); e.hasMoreElements();) {
            String key = e.nextElement().toString();
            NSSelector selector = (NSSelector)self.objectForKey(key);
            orderings.addObject(new EOSortOrdering(key, selector));
        }
        return orderings;
    }
    
}
